<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图实战</title>
    <style>
        .carousel-container {
            position: relative;
            width: 800px;
            height: 400px;
            margin: 0 auto;
            overflow: hidden;
        }
        .carousel-track {
            position: fixed;
            transition: transform 0.5s ease-in-out;
            height: 100%;
        }
        .carousel-track img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            flex-shrink: 0;
            opacity: 0;
            transition: opacity 0.5s ease-in-out;
        }
        .carousel-track img.active {
            opacity: 1;
        }
        /* 控制按钮样式 */
        .prev-btn, .next-btn
         {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            background: rgba(0, 0, 0, 0.5);
            color: #fff;
        }
        /* 指示器样式 */
        .indicator{
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            display: flex;
        }
    </style>
</head>
<script>
    const track = document.querySelector(".carousel-track");
    const images=document.querySelectorAll(".carousel-track img");
    const prevBtn=document.querySelector(".prev-btn");
    const nextBtn=document.querySelector(".next-btn");
    const indicators=document.querySelectorAll(".indicator");
    let currentIndex=0;
    let autoPlayInterval;
    // 创建指示器
    function createIndicators(){
        indicators.forEach((indicator,index)=>{
            indicator.addEventListener("click",()=>{
                currentIndex=index;
                updateCarousel();
            })
        })
        // 切换幻灯片核心逻辑
        function updateCarousel(){
            track.style.transform=`translateX(-${currentIndex*100}%)`;
            images.forEach((image,index)=>{
                image.classList.toggle("active",index===currentIndex);
            })
        }
    }
    // 按钮点击事件绑定
    prevBtn.addEventListener("click",()=>{
        currentIndex--;
        updateCarousel();
    })
    nextBtn.addEventListener("click",()=>{
        currentIndex++;
        updateCarousel();
    })
    // 自动播放
    function startAutoPlay(){
        autoPlayInterval=setInterval(()=>{
            currentIndex++;
            updateCarousel();
        },3000);
    }

</script>
<body>
    
</body>
</html>